<template>
  <div class="auto form-horizontal" >
    <modal :show.sync="show"  v-ref:modal large backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h3 style="color:black" class="modal-title">收费发票</h3>
      </header>
      <article slot="modal-body" style = "display: flex;justify-content: center;" id="bill">
        <div v-if="bill.data">
          {{{ bill.data}}}
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <div v-if="isEnableBill">
          <div class="col-sm-5" v-if="messShow">
            <span style="color: red"><strong>未获取到收据代码，发票不能正常打印，请确认后补打发票</strong></span>
          </div>
          <div class="col-sm-2" v-if="!messShow">
            <span><strong>发票号:{{model.f_bill_id}}</strong></span>
          </div>
        </div>
        <report-print id='bill' top='25mm' left='60mm' width='100%' height='80%' style='td,th{font-size: 10pt!important;}'
         :notrepeat="true" v-if= 'bill.data'></report-print>
        <button type="button" class="btn btn-default" @click='cancel()'id="common-priBill-取消">取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>
import * as Util from '../../../components/Util'
import AppData from '../../../stores/AppData'
import { DataModel } from 'vue-client'
import co from 'co'

let getBillGen = function * (self) {
  let data = {
    items: '*',
    condition: `f_user = '${Util.f.name}' and f_bill_state = '启用' and f_bill_type = '${self.data.f_bill_style}'`,
    tablename: 't_billcollec',
    orderitem: 'id'
  }
  console.log('查询票据票号。。', Util.f.name, self.data.f_bill_style)
  let res = yield self.$SqlService.singleTableOrderBy(data)
  // 判断发票管理是否启用
  if (AppData.getSingleValue('发票管理') === '启用') {
    self.isEnableBill = true
    if (res.data.length > 0) {
      if (res.data[0].f_bill_id > res.data[0].f_end_number) {
        res.data[0].f_bill_state = '停用'
        yield self.$resetpost('rs/entity/t_billcollec', res.data[0], {resolveMsg: null, rejectMsg: null})
        self.messShow = true
      } else {
        self.model.f_bill_id = res.data[0].f_bill_id
        self.model.f_billcoll_id = res.data[0].id
      }
    } else {
      // 提示没有分配发票号段
      self.$showAlert('收费成功。但发票号段未分配, 请联系相关人员分配发票！稍后可进行票据补打进行打票', 'warning', 5000)
      self.$dispatch('toggle')
    }
  } else {
    self.isEnableBill = false
    self.model.f_bill_id = self.data.id
  }
}

export default {
  title: '收费发票',
  data () {
    return {
      model: {
        f_bill_id: null,
        f_billcoll_id: null
      },
      bill: Object,
      // bill: new DataModel('rs/report/machine_bill', {reprint: "'正常'"}),
      // prebill: new DataModel('rs/report/pre_sell', {reprint: "'正常'"}),
      // cardbill: new DataModel('rs/report/card_bill', {reprint: "'正常'"}),
      // billUser: new DataModel('rs/report/machine_user_bill', {reprint: "'正常'"}),
      messShow: false,
      isEnableBill: false
    }
  },
  props: ['show', 'data', 'billUrl'],
  methods: {
    getBillNumber () {
      // 获取是否启用发票管理
      co(getBillGen(this))
    },
    cancel  () {
      this.$dispatch('toggle')
    }
  },
  watch: {
    'show' (val) {
      if (val) {
        console.log('看看打票里面的data', this.data)
        this.bill = new DataModel(this.billUrl, {reprint: "'正常'"})
        this.bill.search(this.data.id)
        // 获取发票号
        this.getBillNumber()
      }
    }
  },
  events: {
    'print' () {
      // 对票据明细进行更新或插入操作
      let data = {
        billcoll: {},
        detail: {
          f_bill_id: this.model.f_bill_id,
          f_userinfo_id: this.data.f_userinfo_id,
          f_user_id: this.data.f_user_id,
          f_user_name: this.data.f_user_name,
          f_billdetail_id: this.data.id,
          f_card_id: this.data.f_card_id,
          f_bill_type: this.data.f_bill_type,
          f_bill_state: '已用',
          f_total_money: this.data.f_oughtfee,
          f_paid_money: this.data.f_collection,
          f_operator: Util.f.name,
          f_filiale: Util.f.f_fengongsi,
          f_outlets: Util.f.f_parentname,
          f_bill_style: this.data.f_print[0]
        },
        type: '打印发票'
      }
      if (this.model.f_billcoll_id) {
        data.billcoll =  {
          id: this.model.f_billcoll_id,
          f_bill_id: this.model.f_bill_id + 1
        }
        this.data.f_billcoll_id = this.model.f_billcoll_id
      }
      this.$resetpost('rs/logic/printBill', {data: data}, {resolveMsg: '打印发票成功', rejectMsg: '打印发票失败'}).then((res) => {
        this.$dispatch('success', '发票打印', this.data)
      }).catch((err) => {
        console.log('发票打印成功，更新发票数据出错。。。', err)
        this.$dispatch('error', '发票打印', this.data, null)
      })
    },
    'print-error' () {
      this.$dispatch('error', '发票打印', this.data, null)
    }
  }
}

</script>
<style >
</style>
